﻿Imports System.Configuration
Imports System.Management
Imports System.Data.SqlClient


Public Class TempiLavoro
    Private connectionstring As String
    Private myconnection As SqlConnection


    Private Sub TempiLavoro_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim test As Boolean

        Me.Text = "Gestione Tempi di Lavoro"
        Me.Size = Utility.DimensioniMonitor()
        Me.Location = New System.Drawing.Point(0, 0)
        Utility.GrideExSetup(GridTempiLavoro, Me)

        test = True

        While test
            Try
                test = False
                connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings("FormFlussi.My.MySettings.CM_DBConnectionString").ConnectionString
                myconnection = New SqlConnection(connectionstring)
                myconnection.Open()

            Catch ex As Exception
                MsgBox("Database non trovato!")
                Config.ShowDialog()
                test = True

            End Try
        End While


        Try


            GridTempiLavoro.SetDataBinding(CM_DBDataSet1, "LavoroSottoprocedure")
            Me.LavoroSottoprocedureTableAdapter1.Fill(Me.CM_DBDataSet1.LavoroSottoprocedure)


        Catch ex As Exception
            Utility.MessaggioErrore("Errore", ex, "Gestione Tempi di Lavoro")

            'MsgBox(ex.Message.ToString)
        End Try

        GridTempiLavoro.MoveFirst()
    End Sub

    Private Sub ButtonOK_Click(sender As System.Object, e As System.EventArgs) Handles ButtonOK.Click
        SalvaModifiche()
        Me.Close()
    End Sub

    Private Sub SalvaModifiche()
        Dim changedRecords As System.Data.DataTable
        Dim SqlStr As String
        Dim SQLcomando As SqlCommand
        Dim RecordModificati As Integer = 0


        changedRecords = CM_DBDataSet1.LavoroSottoprocedure.GetChanges()

        If Not (changedRecords Is Nothing) AndAlso _
            (changedRecords.Rows.Count > 0) Then
            Dim message As String
            message = String.Format("Stai per modificare {0} record(s)." _
                + vbCrLf + "Vuoi Continuare?", changedRecords.Rows.Count)

            If MessageBox.Show(message, "Gestione Tempi di Lavoro", MessageBoxButtons.YesNo, _
                    MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.Yes Then
                Try
                    For x As Int16 = 0 To changedRecords.Rows.Count - 1

                        SqlStr = "update IP_LAVORO_SOTTOPROCEDURE set LavoroNecessario=@LavoroNecessario " & _
                            "where CodSottoprocedura=@CodSottoprocedura and Codlaboratorio=@CodLaboratorio"


                        SQLcomando = New SqlCommand(SqlStr, myconnection)
                        SQLcomando.Parameters.AddWithValue("@LavoroNecessario", changedRecords.Rows(x).Item("LavoroNecessario"))
                        SQLcomando.Parameters.AddWithValue("@CodSottoprocedura", changedRecords.Rows(x).Item("CodSottoprocedura"))
                        SQLcomando.Parameters.AddWithValue("@CodLaboratorio", changedRecords.Rows(x).Item("CodLaboratorio"))


                        RecordModificati = RecordModificati + SQLcomando.ExecuteNonQuery()

                    Next

                    message = String.Format("Sono stati modificati {0} record(s).", RecordModificati)
                    MessageBox.Show(message, "Gestione Tempi di lavoro", MessageBoxButtons.OK)

                Catch exc As Exception
                    Utility.MessaggioErrore("Si è verificato un errore nell'aggiornamento del db", exc, "Gestione Tempi di Lavoro")
                End Try
            End If

        End If
    End Sub

    Private Sub ButtonAnnulla_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAnnulla.Click
        Dim risultato As Integer = MsgBox("Vuoi uscire senza modifiche?", vbYesNo)

        If risultato = 6 Then
            Principale.Show()
            Me.Close()

        End If
    End Sub

    Private Sub BtnExpColl_Click(sender As System.Object, e As System.EventArgs) Handles BtnExpColl.Click
        If GridTempiLavoro.GroupMode.ToString = "Expanded" Then
            GridTempiLavoro.GroupMode = Janus.Windows.GridEX.GroupMode.Collapsed
            GridTempiLavoro.CollapseGroups()
        Else
            GridTempiLavoro.GroupMode = Janus.Windows.GridEX.GroupMode.Expanded
            GridTempiLavoro.ExpandGroups()
        End If
    End Sub
End Class